package com.example.administrator.meishi.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.meishi.R;
import com.example.administrator.meishi.bean.Person;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class RegisterActivity extends AppCompatActivity {

    private EditText register_user;
    private EditText register_pass;
    private String num;
    private EditText yzm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        register_user = (EditText) findViewById(R.id.register_user);
        register_pass = (EditText) findViewById(R.id.register_pass);
        yzm = (EditText) findViewById(R.id.yzm);
    }

    public void getyzm(View view) {
        num = register_user.getText().toString();
        BmobSMS.requestSMSCode(num, "美视", new QueryListener<Integer>() {
            @Override
            public void done(Integer integer, BmobException e) {
                if(e==null){
                    Toast.makeText(RegisterActivity.this, "验证码发送成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void register(View view) {
        String yan = yzm.getText().toString();
        BmobSMS.verifySmsCode(num, yan, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    String pass = register_pass.getText().toString();
                    Person p2 = new Person();
                    p2.setUser(num);
                    p2.setPass(pass);
                    p2.save(new SaveListener<String>() {
                        @Override
                        public void done(String objectId,BmobException e) {
                            if(e==null){
                                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();

                            }
                        }
                    });
                }else{
                    Toast.makeText(RegisterActivity.this, "验证码输入有误", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
